using System;
using Avanzis.QueryBuilder;

namespace Avanzis.QueryObjects
{
    public class ActiveRecordBase<T> : ActiveRecordBase
    {
        public ActiveRecordBase()
        {
        }

        public ActiveRecordBase(Session session)
        {
            this.Session = session;
        }

        public static void Save(T instance)
        {
            ActiveRecordBase.Save(instance);
        }

        public static T FindByPrimaryKey(object id)
        {
            return (T) ActiveRecordBase.FindByPrimaryKey(typeof (T), id);
        }

        public static T FindByPrimaryKey(object id, bool throwOnNotFound)
        {
            return (T) ActiveRecordBase.FindByPrimaryKey(typeof (T), id, throwOnNotFound);
        }

        public static T FindByPrimaryKey(object id, Session session)
        {
            return (T) ActiveRecordBase.FindByPrimaryKey(typeof (T), id, true, session);
        }

        public static T FindByPrimaryKey(object id, bool throwOnNotFound, Session session)
        {
            return (T) ActiveRecordBase.FindByPrimaryKey(typeof (T), id, throwOnNotFound, session);
        }

        public static T[] FindAll()
        {
            return (T[]) ActiveRecordBase.FindAll(typeof (T), null, (SearchFiltersCollection) null);
        }

        public static T[] FindAll(OrderItemsCollection orders)
        {
            return (T[]) ActiveRecordBase.FindAll(typeof (T), orders, null);
        }

        public static T[] FindAll(SearchFiltersCollection filters)
        {
            return (T[]) ActiveRecordBase.FindAll(typeof (T), null, filters);
        }

        public static T[] FindAll(OrderItemsCollection orders, SearchFiltersCollection filters)
        {
            return (T[]) ActiveRecordBase.FindAll(typeof (T), orders, filters);
        }

        public static T[] FindAll(string orderExpression, string filterExpression)
        {
            return (T[]) ActiveRecordBase.FindAll(typeof (T), orderExpression, filterExpression);
        }

        public static T[] FindAllOrdered(string orderExpression)
        {
            return (T[]) ActiveRecordBase.FindAll(typeof (T), orderExpression, null);
        }

        public static T[] FindAll(string filterExpression)
        {
            return (T[]) ActiveRecordBase.FindAll(typeof (T), null, filterExpression);
        }

        public static T FindOne(SearchFiltersCollection filters)
        {
            return (T) ActiveRecordBase.FindOne(typeof (T), filters, false);
        }

        public static T FindOne(SearchFiltersCollection filters, OrderItemsCollection order)
        {
            return (T) ActiveRecordBase.FindOne(typeof (T), order, filters, false);
        }

        #region SlicedFindAll

        public static T[] SlicedFindAllOrdered(int firstResult, int maxResults, string orderExpression)
        {
            return (T[]) ActiveRecordBase.SlicedFindAllOrdered(typeof (T), firstResult, maxResults, orderExpression);
        }

        public static T[] SlicedFindAll(int firstResult, int maxResults)
        {
            return (T[]) ActiveRecordBase.SlicedFindAll(typeof (T), firstResult, maxResults, null, null);
        }

        public static T[] SlicedFindAll(int firstResult, int maxResults, OrderItemsCollection orders)
        {
            return (T[]) ActiveRecordBase.SlicedFindAll(typeof (T), firstResult, maxResults, orders, null);
        }

        public static T[] SlicedFindAll(int firstResult, int maxResults, SearchFiltersCollection filters)
        {
            return (T[]) ActiveRecordBase.SlicedFindAll(typeof (T), firstResult, maxResults, null, filters);
        }

        public static T[] SlicedFindAll(int firstResult, int maxResults, OrderItemsCollection orders, SearchFiltersCollection filters)
        {
            return (T[]) ActiveRecordBase.SlicedFindAll(typeof (T), firstResult, maxResults, orders, filters);
        }

        #endregion

        #region CountAll

        public static int CountAll()
        {
            return ActiveRecordBase.CountAll(typeof (T), (SearchFiltersCollection) null);
        }

        public static int CountAll(SearchFiltersCollection filters)
        {
            return ActiveRecordBase.CountAll(typeof (T), filters);
        }

        public static int CountAll(string filterExpression)
        {
            return ActiveRecordBase.CountAll(typeof (T), filterExpression);
        }

        #endregion

        #region SelectBuilder

        public static SelectBuilder GetSelectBuilder()
        {
            return GetSelectBuilder(null, null);
        }

        public static SelectBuilder GetSelectBuilder(OrderItemsCollection orders, SearchFiltersCollection filters)
        {
            return ActiveRecordBase.GetSelectBuilder(typeof (T), orders, filters);
        }
      

        #endregion

        #region Schema

        /// <summary>
        /// Gets the schema.
        /// </summary>
        /// <param name="objectType">Type of the object.</param>
        /// <returns></returns>
        public static QueryObjectSchema GetSchema(Type objectType)
        {
            return QueryObjectSchema.GetSchema(objectType);
        }

        #endregion

        public static void Delete(T instance)
        {
            ActiveRecordBase.Delete(instance);
        }
    }
}